package dca;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.util.regex.Pattern;

public class PatientRegistry {
	
	private ArrayList<Patient> PatientList = new ArrayList<Patient>();
	private String filename;
	
	public PatientRegistry(){};
	public PatientRegistry(String filename) throws IOException{
		this.filename = filename;
		loadPatients(this.filename);
	}
	
	
	/**
	 * Reads a text file of patients and loads the patients into the registry.
	 * @return
	 */
	public void loadPatients(String filename) throws IOException,BadDataException{
		File inFile = new File("data/"+filename+".txt");
		Scanner in = new Scanner(inFile);
		try
		{
		readData(in);
		}
		finally
		{
		in.close();			
		}
		
	}
	
	private void readData(Scanner in) throws BadDataException{
		while(in.hasNextLine()){
			String IDLine = in.nextLine();
			String symptomLine = in.nextLine();
			Pattern P = Pattern.compile("[:%]+");
			Pattern S = Pattern.compile("[,\n]+");
			String[] ID = P.split(IDLine);
			String[] symptoms = S.split(symptomLine);
			
			ArrayList<Symptom> readSymptoms = new ArrayList<Symptom>();
			for(int i=0;i<symptoms.length;i+=2)
			{
				readSymptoms.add(new Symptom(symptoms[i],symptoms[i+1]));
			}
			Patient aPatient = new Patient(ID[0],Integer.parseInt(ID[1].trim()),readSymptoms);
			
			//Cured patients are removed from the datafile on load. In this way I can keep track of cured patients
			//while having them removed between sessions.
			if(!(aPatient.getHealth()>=Patient.max_healt))  
				PatientList.add(aPatient);
		}
	}
	
	/**
	 * Adds a new patient to the registry if it does not already exists.
	 * @param Name
	 * @param level
	 * @throws BadDataException 
	 */
	public void addPatient(Patient aPatient) throws BadDataException{
		for(Patient s:PatientList)
		{
			if(s.getName().equalsIgnoreCase(aPatient.getName()))
				return;
		}
		PatientList.add(aPatient);
	}
	
	public void removePatient(Patient aPatient){
		PatientList.remove(aPatient);
	}
	
	/**
	 * Saves the current patients to a text file.
	 * @throws FileNotFoundException 
	 */
	public void savePatients(String filename) throws FileNotFoundException{
		PrintWriter out = new PrintWriter("data/"+filename+".txt");
		for(Patient p:PatientList)
		{
			out.println(p.toString());
		}
		out.close();
	}
	public void savePatients() throws FileNotFoundException{
		this.savePatients(this.filename);
	}
	
	/**
	 * Finds a patient by name.
	 * @param Name
	 * @return
	 */
	public Patient getPatient(String Name){
		for(Patient s:PatientList)
			if (s.getName().equalsIgnoreCase(Name))
				return s;
		throw new NoSuchElementException("Patient "+Name+". Does not exsist in patient registry.");
	}
	
	
	/**
	 * Returns a list of all patients in alphabetical order.
	 */
	public ArrayList<Patient> PatientList(){
		Collections.sort(PatientList);
		return PatientList;
	}
	

	
	//Useful for adding patients to a database or checking where you are working.
	public String getFilename(){
		return this.filename;
	}

	
}
